package source;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JSpinner;
import javax.swing.JButton;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SpinnerNumberModel;

import source.RobotControl.ExecutionState;

public class Test {

	private JFrame frame;
	private JTextField timeInp;
	private JTextField vLInp;
	private JTextField vRInp;
	
	private JTextField timeCmdInp;
	private JTextField vLCmdInp;
	private JTextField vRCmdInp;
	
	private JLabel execStateLbl;
	
	private JSpinner portNumSelector;
	private JLabel lblConOrDisc;
	private JLabel lblNotOverwriting;
	
	private static RobotControl robotControl;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		robotControl = new RobotControl();
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Test window = new Test();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Test() {
		initialize();
	}

	private void connectToPort(){
		try {
		robotControl.connect((Integer)portNumSelector.getValue());
		} catch (Exception e){
			//e.printStackTrace();
		}
		
		if (robotControl.getConnection() == ( RobotControl.Connection.connected)){
			lblConOrDisc.setForeground(Color.GREEN);
			lblConOrDisc.setText("Connected");
		}
	}
	
	private void closeConnection(){
		robotControl.disconnect();
		
		if (robotControl.getConnection() == ( RobotControl.Connection.disconnected)){
			lblConOrDisc.setForeground(Color.RED);
			lblConOrDisc.setText("Disconnected");
		}
	}
	
	private void start(){
		robotControl.startRunning();
	}
	
	private void stop(){
		robotControl.stopRunning();
	}
	
	private void addCmd(){	
		StateAndCommand sac = robotControl.addCommand(	Double.parseDouble(String.valueOf(vLInp.getText())),
														Double.parseDouble(String.valueOf(vRInp.getText())),
														Integer.parseInt(timeInp.getText()));
		
		timeCmdInp.setText(Integer.toString(sac.getCommand().getTime()));
		vLCmdInp.setText(trunc(Double.toString(sac.getCommand().getV(Enums.Side.LEFT))));
		vRCmdInp.setText(trunc(Double.toString(sac.getCommand().getV(Enums.Side.RIGHT))));
	}
	
	private void overwrite(){
		robotControl.setControlMode(RobotControl.ControlMode.changeToIdeal);
		
		lblNotOverwriting.setForeground(Color.GREEN);
		lblNotOverwriting.setText("Overwriting");
	}
	
	private void dontOverwrite(){
		robotControl.setControlMode(RobotControl.ControlMode.withoutModification);
		
		lblNotOverwriting.setForeground(Color.RED);
		lblNotOverwriting.setText("Not Overwriting");
	}
	
	private void getExecState(){
		ExecutionState execState = robotControl.getExecutionState();
		
		if (execState == ExecutionState.ready){
			execStateLbl.setText("Ready");
		} else {
			execStateLbl.setText("Running");
		}
	}
	
	public String trunc(String s){
		String ret = s;
		
		int decimalIndex = s.indexOf('.');
		int eIndex = s.indexOf('E');
		
		if (decimalIndex != -1){
			ret = s.substring(0, Math.min(decimalIndex + 4, s.length()));
			if (eIndex != -1){
				ret = ret.concat(s.substring(s.length()-3, s.length()));
			}
		}
		
		return ret;
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		Border line = BorderFactory.createLineBorder(Color.gray);
		
		frame = new JFrame();
		frame.setBounds(100, 100, 524, 544);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel portConnPanel = new JPanel();
		portConnPanel.setBorder(new TitledBorder(line, "Port connection", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		portConnPanel.setBounds(10, 11, 488, 71);
		frame.getContentPane().add(portConnPanel);
		portConnPanel.setLayout(null);
		
		JLabel lblPortNumber = new JLabel("Port number:");
		lblPortNumber.setBounds(10, 22, 80, 33);
		portConnPanel.add(lblPortNumber);
		lblPortNumber.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		portNumSelector = new JSpinner();
		portNumSelector.setModel(new SpinnerNumberModel(new Integer(18), null, null, new Integer(1)));
		portNumSelector.setBounds(100, 29, 47, 20);
		portConnPanel.add(portNumSelector);
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				connectToPort();
			}
		});
		btnConnect.setBounds(179, 28, 89, 23);
		portConnPanel.add(btnConnect);
		
		JButton btnDisconnect = new JButton("Disconnect");
		btnDisconnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeConnection();
			}
		});
		btnDisconnect.setBounds(292, 27, 98, 23);
		portConnPanel.add(btnDisconnect);
		
		lblConOrDisc = new JLabel("Disconnected");
		lblConOrDisc.setBounds(402, 22, 80, 33);
		portConnPanel.add(lblConOrDisc);
		lblConOrDisc.setForeground(Color.RED);
		lblConOrDisc.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JPanel cmdInpPanel = new JPanel();
		cmdInpPanel.setBorder(new TitledBorder(line, "Command inputs", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		cmdInpPanel.setBounds(10, 183, 492, 107);
		frame.getContentPane().add(cmdInpPanel);
		cmdInpPanel.setLayout(null);
		
		timeInp = new JTextField();
		timeInp.setBounds(31, 60, 89, 23);
		cmdInpPanel.add(timeInp);
		timeInp.setColumns(10);
		
		JLabel lblTime = new JLabel("Time [ms]");
		lblTime.setBounds(31, 29, 89, 20);
		cmdInpPanel.add(lblTime);
		lblTime.setHorizontalAlignment(SwingConstants.CENTER);
		lblTime.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JLabel lblVL = new JLabel("V left [m/s]");
		lblVL.setBounds(155, 29, 89, 20);
		cmdInpPanel.add(lblVL);
		lblVL.setHorizontalAlignment(SwingConstants.CENTER);
		lblVL.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		vLInp = new JTextField();
		vLInp.setBounds(155, 60, 89, 23);
		cmdInpPanel.add(vLInp);
		vLInp.setColumns(10);
		
		vRInp = new JTextField();
		vRInp.setBounds(281, 60, 89, 23);
		cmdInpPanel.add(vRInp);
		vRInp.setColumns(10);
		
		JLabel lblVR = new JLabel("V right [m/s]");
		lblVR.setBounds(281, 29, 89, 20);
		cmdInpPanel.add(lblVR);
		lblVR.setHorizontalAlignment(SwingConstants.CENTER);
		lblVR.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addCmd();
			}
		});
		btnAdd.setBounds(393, 11, 89, 23);
		cmdInpPanel.add(btnAdd);
		
		JButton btnStart = new JButton("Start");
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				start();
			}
		});
		btnStart.setBounds(393, 45, 89, 23);
		cmdInpPanel.add(btnStart);
		
		JButton btnStop = new JButton("Stop");
		btnStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				stop();
			}
		});
		btnStop.setBounds(393, 73, 89, 23);
		cmdInpPanel.add(btnStop);
		
		JPanel overwritingPanel = new JPanel();
		overwritingPanel.setBorder(new TitledBorder(line, "Overwriting command", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		overwritingPanel.setBounds(10, 94, 409, 78);
		frame.getContentPane().add(overwritingPanel);
		overwritingPanel.setLayout(null);
		
		JButton btnOverwrite = new JButton("Overwrite");
		btnOverwrite.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				overwrite();
			}
		});
		btnOverwrite.setBounds(10, 32, 105, 23);
		overwritingPanel.add(btnOverwrite);
		
		JButton btnDontOverwrite = new JButton("Don't Overwrite");
		btnDontOverwrite.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dontOverwrite();
			}
		});
		btnDontOverwrite.setBounds(136, 32, 132, 23);
		overwritingPanel.add(btnDontOverwrite);
		
		lblNotOverwriting = new JLabel("Not Overwriting");
		lblNotOverwriting.setBounds(278, 31, 105, 23);
		overwritingPanel.add(lblNotOverwriting);
		lblNotOverwriting.setHorizontalAlignment(SwingConstants.CENTER);
		lblNotOverwriting.setForeground(Color.RED);
		lblNotOverwriting.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JPanel GivenCmdPanel = new JPanel();
		GivenCmdPanel.setLayout(null);
		GivenCmdPanel.setBorder(new TitledBorder(line, "Given Command", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GivenCmdPanel.setBounds(10, 301, 409, 107);
		frame.getContentPane().add(GivenCmdPanel);
		
		timeCmdInp = new JTextField();
		timeCmdInp.setEditable(false);
		timeCmdInp.setColumns(10);
		timeCmdInp.setBounds(30, 57, 89, 23);
		GivenCmdPanel.add(timeCmdInp);
		
		JLabel label = new JLabel("Time [ms]");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("Tahoma", Font.PLAIN, 13));
		label.setBounds(30, 26, 89, 20);
		GivenCmdPanel.add(label);
		
		JLabel label_1 = new JLabel("V left [m/s]");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		label_1.setBounds(154, 26, 89, 20);
		GivenCmdPanel.add(label_1);
		
		vLCmdInp = new JTextField();
		vLCmdInp.setEditable(false);
		vLCmdInp.setColumns(10);
		vLCmdInp.setBounds(154, 57, 89, 23);
		GivenCmdPanel.add(vLCmdInp);
		
		vRCmdInp = new JTextField();
		vRCmdInp.setEditable(false);
		vRCmdInp.setColumns(10);
		vRCmdInp.setBounds(280, 57, 89, 23);
		GivenCmdPanel.add(vRCmdInp);
		
		JLabel label_2 = new JLabel("V right [m/s]");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setFont(new Font("Tahoma", Font.PLAIN, 13));
		label_2.setBounds(280, 26, 89, 20);
		GivenCmdPanel.add(label_2);
		
		JPanel execStatePanel = new JPanel();
		execStatePanel.setLayout(null);
		execStatePanel.setBorder(new TitledBorder(line, "Execution State", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		execStatePanel.setBounds(10, 420, 269, 78);
		frame.getContentPane().add(execStatePanel);
		
		JButton execStateBtn = new JButton("Get");
		execStateBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getExecState();
			}
		});
		execStateBtn.setBounds(10, 32, 105, 23);
		execStatePanel.add(execStateBtn);
		
		execStateLbl = new JLabel("Ready");
		execStateLbl.setHorizontalAlignment(SwingConstants.CENTER);
		execStateLbl.setForeground(Color.BLACK);
		execStateLbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		execStateLbl.setBounds(141, 31, 105, 23);
		execStatePanel.add(execStateLbl);
	}

	
}
